package com.tank.demo.frame;

import com.tank.demo.abstractFactory.BaseTank;
import com.tank.demo.facadeAndMediator.GameModel;
import com.tank.demo.myenum.DirEnum;
import com.tank.demo.strategy.FireStrategy;
import lombok.*;

import java.awt.*;
import java.awt.event.*;

import static com.tank.demo.utils.PropertyMgr.getInstance;

@Getter
@Setter
@AllArgsConstructor
public class TankFrame extends Frame {

    public TankFrame() {
        this.setSize(GameModel.GAME_WIDTH, GameModel.GAME_HEIGHT);
        this.setTitle("Tank War");
        this.setResizable(false);
        this.setVisible(true);
        this.addKeyListener(new MyKetListener());
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
    class MyKetListener extends KeyAdapter {

        boolean bleft = false;
        boolean bright = false;
        boolean bup = false;
        boolean bdown = false;

        public MyKetListener(){

        }

        @Override
        public void keyPressed(KeyEvent e) {
            int key = e.getKeyCode();
            switch (key) {
                case KeyEvent.VK_LEFT:
                    bleft = true;
                    break;
                case KeyEvent.VK_RIGHT:
                    bright = true;
                    break;
                case KeyEvent.VK_UP:
                    bup = true;
                    break;
                case KeyEvent.VK_DOWN:
                    bdown = true;
                    break;
                case KeyEvent.VK_CONTROL:
                    try {
                        GameModel.getInstance().myTank.fire((FireStrategy) Class.forName(getInstance().getKey("myTankFireStrategy")).newInstance());
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                    break;
                default:
                    break;
            }
            setTankDirction();
        }

        @Override
        public void keyReleased(KeyEvent e) {
            int key = e.getKeyCode();
            switch (key) {
                case KeyEvent.VK_LEFT:
                    bleft = false;
                    break;
                case KeyEvent.VK_RIGHT:
                    bright = false;
                    break;
                case KeyEvent.VK_UP:
                    bup = false;
                    break;
                case KeyEvent.VK_DOWN:
                    bdown = false;
                    break;
                default:
                    break;
            }
            setTankDirction();
        }

        private void setTankDirction() {
            BaseTank myTank = GameModel.getInstance().myTank;
            if (!bleft && !bright && !bup && !bdown) myTank.setMoving(false);
            else {
                if (bleft) myTank.setDir(DirEnum.LEFT);
                if (bright) myTank.setDir(DirEnum.RIGHT);
                if (bup) myTank.setDir(DirEnum.UP);
                if (bdown) myTank.setDir(DirEnum.DOWN);
                myTank.setMoving(true);

            }
        }

    }


    Image offScreanimage = null;

    /**
     * 双缓冲取消闪烁
     *
     * @param g
     */
    @Override
    public void update(Graphics g) {
        if (offScreanimage == null) {
            offScreanimage = this.createImage(GameModel.GAME_WIDTH, GameModel.GAME_HEIGHT);
        }
        Graphics gScreangraphics = offScreanimage.getGraphics();
        Color color = gScreangraphics.getColor();
        gScreangraphics.setColor(Color.black);
        gScreangraphics.fillRect(0, 0, GameModel.GAME_WIDTH, GameModel.GAME_HEIGHT);
        gScreangraphics.setColor(color);
        paint(gScreangraphics);
        g.drawImage(offScreanimage, 0, 0, null);

    }

    @Override
    public void paint(Graphics g) {
        GameModel.getInstance().paint(g);
    }


}
